//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "PostEffect_DepthOfFeild" {
	Parameters{	
	Vector [f1] = "0.0,0.0,0.0,0.0"
	Vector [f2] = "0.0,0.0,0.0,0.0"
	Vector [f3] = "0.0,0.0,0.0,0.0"
	Float [focalDistance] = "16.0"
	Float [fHyperfocalDistance] = "0.02"
	Float [fMaxBlurFact] = "1.0"
	}
	
	RenderQueue "CUSTOMDRAW"
	
	

			CODEBEGIN

			
			float4 f1;
			float4 f2;
			float4 f3;
			
			float  focalDistance;
			float  fHyperfocalDistance;
			float  fMaxBlurFact;
			
			
			struct VS_OUTPUT
			{
				float4 Position		: POSITION0;   // vertex position 
				float4 PosTex		: TEXCOORD0;//xy is positon, and zw is texCoords coord
			};
			struct PS_INPUT
			{
				float4 PosTex		: TEXCOORD0;
			};
			
			VS_OUTPUT mainVs(in float3 vPos : POSITION)
			{
				VS_OUTPUT Output;
								
				Output.Position.xy = vPos.xy;	
				Output.Position.zw = float2(0.9f, 1.0f);
				Output.PosTex.xy = vPos.xy;
				Output.PosTex.zw = 0;
				return Output;
			}

			float4 mainPs(PS_INPUT o) : COLOR0
			{
				float2 texCoords = getUVfromscreenspace(o.PosTex.xy);
				float4 vViewPosition;
				vViewPosition.x = g_CameraPos.x;
				vViewPosition.y = g_CameraPos.y;
				vViewPosition.z = g_CameraPos.z;
				vViewPosition.w = 1.0;
				
				float4 color = tex2D(g_MainBuffer,texCoords );
				
				float4 depthcolor = tex2D(g_DepthMap,texCoords);
				//-hack for the sky box's depth is 0
				if(depthcolor.r<0.000001)
				{
					depthcolor.r = 1.0;
				}	
				
				float dis = (g_CameraParam.y - g_CameraParam.x) * depthcolor.r;
							
				float blur = (dis - focalDistance)*fHyperfocalDistance;
				blur = blur*blur;
				blur = min(blur,fMaxBlurFact);
				
				float2 texcoords1;
				texcoords1.x=texCoords.x+f1.x;
				texcoords1.y=texCoords.y+f1.y;
				float4 Current1 = tex2D(g_MainBuffer, texcoords1);
				float4 depth1 = tex2D(g_DepthMap,texcoords1);
				//-hack for the sky box's depth is 0
				if(depth1.r<0.000001)
				{
					depth1.r = 1.0;
				}	
				float dis1 = (g_CameraParam.y - g_CameraParam.x)*depth1.r;
				float blur1 = (dis1 - focalDistance)*fHyperfocalDistance;
				blur1 = blur1*blur1;
				blur1 = min(blur1,fMaxBlurFact);
				Current1 = color+(Current1-color)*blur1*blur;
				
				float2 texcoords2;
				texcoords2.x=texCoords.x+f1.z;
				texcoords2.y=texCoords.y+f1.w;
				float4 Current2 = tex2D(g_MainBuffer, texcoords2);
				float4 depth2 = tex2D(g_DepthMap,texcoords2);
				//-hack for the sky box's depth is 0
				if(depth2.r<0.000001)
				{
					depth2.r = 1.0;
				}	
				float dis2 = (g_CameraParam.y - g_CameraParam.x)*depth2.r;
				float blur2 = (dis2 - focalDistance)*fHyperfocalDistance;
				blur2 = blur2*blur2;
				blur2 = min(blur1,fMaxBlurFact);
				Current2 = color+(Current2-color)*blur2*blur;
				
				float2 texcoords3;
				texcoords3.x=texCoords.x+f2.x;
				texcoords3.y=texCoords.y+f2.y;
				float4 Current3 = tex2D(g_MainBuffer, texcoords3);
				float4 depth3 = tex2D(g_DepthMap,texcoords3);
				//-hack for the sky box's depth is 0
				if(depth3.r<0.000001)
				{
					depth3.r = 1.0;
				}	
				float dis3 = (g_CameraParam.y - g_CameraParam.x)*depth3.r;
				float blur3 = (dis3 - focalDistance)*fHyperfocalDistance;
				blur3 = blur3*blur3;
				blur3 = min(blur3,fMaxBlurFact);
				Current3 = color+(Current3-color)*blur3*blur;
				
				float2 texcoords4;
				texcoords4.x=texCoords.x+f2.z;
				texcoords4.y=texCoords.y+f2.w;
				float4 Current4 = tex2D(g_MainBuffer, texcoords4);
				float4 depth4 = tex2D(g_DepthMap,texcoords4);
				//-hack for the sky box's depth is 0
				if(depth4.r<0.000001)
				{
					depth4.r = 1.0;
				}	
				float dis4 = (g_CameraParam.y - g_CameraParam.x)*depth4.r;
				float blur4 = (dis4 - focalDistance)*fHyperfocalDistance;
				blur4 = blur4*blur4;
				blur4 = min(blur4,fMaxBlurFact);
				Current4 = color+(Current4-color)*blur4*blur;
				
				float2 texcoords5;
				texcoords5.x=texCoords.x+f3.x;
				texcoords5.y=texCoords.y+f3.y;
				float4 Current5 = tex2D(g_MainBuffer, texcoords5);
				float4 depth5 = tex2D(g_DepthMap,texcoords5);
				//-hack for the sky box's depth is 0
				if(depth5.r<0.000001)
				{
					depth5.r = 1.0;
				}	
				float dis5 = (g_CameraParam.y - g_CameraParam.x)*depth5.r;
				float blur5 = (dis5 - focalDistance)*fHyperfocalDistance;
				blur5 = blur5*blur5;
				blur5 = min(blur5,fMaxBlurFact);
				Current5 = color+(Current5-color)*blur5*blur;
				
				float2 texcoords6;
				texcoords6.x=texCoords.x+f3.z;
				texcoords6.y=texCoords.y+f3.w;	
				float4 Current6 = tex2D(g_MainBuffer, texcoords6);
				float4 depth6 = tex2D(g_DepthMap,texcoords6);
				//-hack for the sky box's depth is 0
				if(depth6.r<0.000001)
				{
					depth6.r = 1.0;
				}
				float dis6 = (g_CameraParam.y - g_CameraParam.x)*depth6.r;
				float blur6 = (dis6 - focalDistance)*fHyperfocalDistance;
				blur6 = blur6*blur6;
				blur6 = min(blur6,fMaxBlurFact);
				Current6 = color+(Current6-color)*blur6*blur;
							
				float4 finalcolor;
							
				finalcolor = (Current1+Current2+Current3+Current4+Current5+Current6)/6;
   				return finalcolor;
   				
			}
			CODEEND
	Technique{
		Pass{
		    Compiler
			          {
				       VertexShader = mainVs
				       PixelShader = mainPs			
			
			          }

			RenderSTATE { 
							cullMode "Back"
							ZTest Off
							ZWrite Off
							blendMode Off
							alphatest Off
							samplerState "g_MainBuffer" [Clamp] [Point]
							samplerState "g_DepthMap"   [Clamp] [Point]
						 }
		}
	}
}







                                                                                            